import {createRouter, createWebHistory} from "vue-router";
import Home from "../views/Home.vue";

const routes = [
    {
        path: '/',
        redirect: '/index'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/index",
                name: "index",
                meta: {
                    title: '首页'
                },
                component: () => import (
                /* webpackChunkName: "index" */
                "../views/index.vue")
            },
            {
                path: "/AlreadyFollow",
                name: "AlreadyFollow",
                meta: {
                    title: '已关注列表'
                },
                component: () => import (
                "../views/follow/AlreadyFollow.vue")
            },
            {
                path: "/AlreadyDetails",
                name: "AlreadyDetails",
                meta: {
                    title: '关注详情'
                },
                component: () => import (
                "../views/follow/AlreadyDetails.vue")
            },
            {
                path: "/ToexamineFollow",
                name: "ToexamineFollow",
                meta: {
                    title: '审核列表'
                },
                component: () => import (
                "../views/follow/ToexamineFollow.vue")
            },
            {
                path: "/ToexamineDetails",
                name: "ToexamineDetails",
                meta: {
                    title: '审核详情'
                },
                component: () => import (
                "../views/follow/ToexamineDetails.vue")
            },
            {
                path: "/ListTask",
                name: "ListTask",
                meta: {
                    title: '任务列表'
                },
                component: () => import (
                "../views/task/ListTask.vue")
            },
            {
                path: "/ReceiveDetails",
                name: "ReceiveDetails",
                meta: {
                    title: '任务明细'
                },
                component: () => import (
                "../views/task/ReceiveDetails.vue")
            },
            {
                path: "/AcceptanceTask",
                name: "AcceptanceTask",
                meta: {
                    title: '任务验收'
                },
                component: () => import (
                "../views/task/AcceptanceTask.vue")
            },
            {
                path: "/SettlementTask",
                name: "SettlementTask",
                meta: {
                    title: '任务结算'
                },
                component: () => import (
                "../views/task/SettlementTask.vue")
            },
            {
                path: "/Billing",
                name: "Billing",
                meta: {
                    title: '财务结算账单'
                },
                component: () => import (
                "../views/finance/Billing.vue")
            },
            {
                path: "/Invoice",
                name: "Invoice",
                meta: {
                    title: '发票管理'
                },
                component: () => import (
                "../views/finance/Invoice.vue")
            },
            {
                path: "/setting",
                name: "setting",
                meta: {
                    title: '账号设置'
                },
                component: () => import (
                "../views/setting.vue")
            }
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import (
        /* webpackChunkName: "login" */
        "../views/Login.vue")
    }, {
        path: "/register",
        name: "register",
        meta: {
            title: '注册'
        },
        component: () => import (
        /* webpackChunkName: "login" */
        "../views/register.vue")
    }, {
        path: "/:pathMatch(.*)*",
		meta: {
			title: '404页面不存在'
		},
        component: () => import (
        /* webpackChunkName: "login" */
        "../views/404.vue")
    }
];

const router = createRouter({
    // history: createWebHistory('/bussiness/'),
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title}`;
    const role = localStorage.getItem('bussines_token');
    if (!role && to.path !== '/login') {
        if(to.path == '/register') {
            // 判断是否是注册
            next();
        }else {
            next('/login');
        }
    } else {
        next();
    }
});

export default router;